using UnityEngine;

public class UIController : MonoBehaviour
{
    [SerializeField] private ShopKeeperDisplay _shopKeeperDisplay;

    private void Awake() {
        _shopKeeperDisplay.gameObject.SetActive(false);
    }

    void Update()
    {
        //activeInHierarchy 检查该对象是否处于活动状态
        if (_shopKeeperDisplay.gameObject.activeInHierarchy && Input.GetKeyDown(KeyCode.Escape))
        {
            // 按下 ESC 键关闭商品界面
            _shopKeeperDisplay.gameObject.SetActive(false);
        }
    }

    // 注册事件监听器，在脚本启用时调用
    private void OnEnable()
    {
        ShopKeeper.OnShopWindowRequested += DisplayShopWindow;
    }

    // 取消事件监听器，在脚本禁用时调用
    private void OnDisable()
    {
        ShopKeeper.OnShopWindowRequested -= DisplayShopWindow;
    }

    // 显示商店窗口的方法，响应ShopKeeper.OnShopWindowRequested事件
    private void DisplayShopWindow(ShopSystem shopSystem, PlayerInventoryHolder playerInventory)
    {
        _shopKeeperDisplay.gameObject.SetActive(true);
        _shopKeeperDisplay.DisplayShopWindow(shopSystem, playerInventory);
    }
}
